package com.jn.jn_client2;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jn.server.IStudentChangeListener;
import com.jn.server.IStudentService;
import com.jn.server.Student;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mTvCount;
    private EditText mEtName;
    private EditText mEtNumber;
    private EditText mEtGrade;
    private EditText mEtAge;
    private EditText mEtRmNumber;
    private Button mBtnAdd;
    private Button mBtnRemove;
    private TextView mTvName;
    private TextView mTvNumber;
    private TextView mTvGrade;
    private TextView mTvAge;
    private EditText mEtInput;
    private Button mBtnSelect;
    private int mStudentCount;
    private IStudentService mService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        mTvCount = findViewById(R.id.count);
        mEtName = findViewById(R.id.name);
        mEtNumber = findViewById(R.id.number);
        mEtGrade = findViewById(R.id.grade);
        mEtAge = findViewById(R.id.age);
        mEtRmNumber = findViewById(R.id.rm_number);
        mBtnAdd = findViewById(R.id.add);
        mBtnRemove = findViewById(R.id.remove);
        mTvName = findViewById(R.id.tv_name);
        mTvNumber = findViewById(R.id.tv_number);
        mTvGrade = findViewById(R.id.tv_grade);
        mTvAge = findViewById(R.id.tv_age);
        mEtInput = findViewById(R.id.input_number);
        mBtnSelect = findViewById(R.id.select);
        mTvCount.setOnClickListener(this);
        mBtnAdd.setOnClickListener(this);
        mBtnRemove.setOnClickListener(this);
        mBtnSelect.setOnClickListener(this);
        bindService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }

    private void bindService() {
        Intent intent = new Intent();
        intent.setPackage("com.jn.server");
        intent.setAction("com.jn.server.action");
        startService(intent);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IStudentService.Stub.asInterface(service);
            try {
                mService.addListener(mListener);
                mStudentCount = mService.getStudentCount();
                mTvCount.setText(mStudentCount + "");
                Log.d("asdf", "onServiceConnected " + mStudentCount);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };

    IStudentChangeListener mListener = new IStudentChangeListener.Stub() {
        @Override
        public void onStudentCountChanged(int count) throws RemoteException {
            mStudentCount = count;
            mTvCount.setText(mStudentCount + "");
        }

        @Override
        public void onStudentNotFound() throws RemoteException {
            Toast.makeText(MainActivity.this, "学号不存在", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onClick(View v) {
        if (mService == null) {
            return;
        }
        int id = v.getId();
        if (id == R.id.add) {
            try {
                String name = mEtName.getText().toString();
                int number = Integer.parseInt(mEtNumber.getText().toString());
                String grade = mEtGrade.getText().toString();
                int age = Integer.parseInt(mEtAge.getText().toString());
                Student student = new Student(name, number, grade, age);
                mService.addStudent(student);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            } catch (NumberFormatException ne) {
                Toast.makeText(this, "请输入正确格式", Toast.LENGTH_SHORT).show();
            }

        } else if (id == R.id.remove) {
            try {
                int rm_number = Integer.parseInt(mEtRmNumber.getText().toString());
                mService.deleteStudent(rm_number);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            } catch (NumberFormatException ne) {
                Toast.makeText(this, "请输入正确学号", Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.count) {
            try {
                mTvCount.setText(mService.getStudentCount() + "");
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        } else if (id == R.id.select) {
            String number = mEtInput.getText().toString();
            if (TextUtils.isEmpty(number)) {
                Toast.makeText(this, "请输入学号", Toast.LENGTH_SHORT).show();
            }
            try {
                int n = Integer.parseInt(number);
                Student student = mService.getStudentByNumber(n);
                if (student != null) {
                    mTvName.setText(student.getName());
                    mTvNumber.setText(student.getNumber() + "");
                    mTvGrade.setText(student.getGrade());
                    mTvAge.setText(student.getAge() + "");
                }
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            } catch (NumberFormatException ne) {
                Toast.makeText(this, "请输入正确学号", Toast.LENGTH_SHORT).show();
            }
        }
    }
}